package com.xiaocq.wordskiller;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class CustomWrongSet extends Activity{
	private Button backToMenu,switchShow;
	private ListView wrongSet;
	private ProgressDialog progressDialog;
	private Cursor cursor;
	private MyAdapter adapter;
	
	private Handler loadingHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			wrongSet.setAdapter(adapter);
			progressDialog.dismiss();
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_wrongset);
		
		backToMenu = (Button) findViewById(R.id.custom_wrongset_backToMenu);
		switchShow = (Button) findViewById(R.id.custom_wrongset_switchShow);
		wrongSet = (ListView) findViewById(R.id.custom_wrongset_wrongSet);
		
		backToMenu.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				finish();
			}
		});
		
		switchShow.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(CustomWrongSet.this, UnitWrongSet.class);
				startActivity(intent);
				finish();
			}
		});
		
		prepareInBackground();
	}
	
	private void prepareInBackground(){
		progressDialog = new ProgressDialog(CustomWrongSet.this);
		progressDialog.setMessage(getText(R.string.tip_loading));
		progressDialog.show();
		
		new Thread(){
			public void run() {
				prepareCursorData();
				loadingHandler.sendEmptyMessage(0);
			};
		}.start();
	}
	
	private void prepareCursorData(){
		ActivityHelper helper = new ActivityHelper(CustomWrongSet.this);
		helper.open();
		cursor = helper.getCursorDataByType(4);
		adapter = new MyAdapter(CustomWrongSet.this, cursor, false);
		helper.close();
	}

}
